//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "safeVehicle.sqf"
#include <tmf\tmf_constants.h>
#include <tmf\tmf_macros.h>
WAITINIT(safe_vehicle);

TRACE_1("Start",_this);
private ["_vehicle","_crew","_safe","_unit","_isWater","_height","_position","_hidden","_i","_timeout_unsafe","_height_limit","_height_limit_half","_fire_unsafe_delay","_unsafe_markers","_unsafe_zones","_shape","_ground_pos"];
DEFAULT_PARAM(_vehicle,0,objNull);
DEFAULT_PARAM(_height_limit,1,-1);
DEFAULT_PARAM(_fire_unsafe_delay,2,300);
DEFAULT_PARAM(_unsafe_markers,3,[]);

_safe=[];
_unsafe_zones = [];
// Whenever the vehicle fires, it becomes unsafe for the next 5 minutes.
_timeout_unsafe = -1;
_height_limit_half = -1;
if (_height_limit > 0) then {
	_height_limit_half = _height_limit / 2;
};
_vehicle setVariable ["TIMEOUT_UNSAFE",_timeout_unsafe];
_vehicle setVariable ["UNSAFE_DELAY",_fire_unsafe_delay];
_vehicle addEventHandler ["fired",{
	// #ifdef __FORCE_DEBUG__
	// ((crew (_this select 0)) select 0) sideChat "Fired";
	// #endif	
	((_this select 0)) setVariable ["TIMEOUT_UNSAFE",time + (((_this select 0)) getVariable ["UNSAFE_DELAY",300])];
}];
if (count _unsafe_markers > 0) then {
	{
		_shape = [_x] call FFUNC(toShape);
		if (((_shape select 0) distance [0,0,0]) > 0 && {(_shape select 1) > 0}) then {
			TRACE_1("Adding Unsafe Zone",_shape);
			_unsafe_zones set [count _unsafe_zones, [(_shape select 0), ((_shape select 1) max (_shape select 2))]];
		};
	} foreach _unsafe_markers;
};
waituntil {
	sleep 3;
	_crew = crew _vehicle;
	_position = position _vehicle;
	_height = _position select 2;
	_ground_pos = _position;
	_ground_pos set [2,0];
	_isWater = surfaceIsWater (position _vehicle);
	_timeout_unsafe = _vehicle getVariable ["TIMEOUT_UNSAFE",-1];
	_hidden = ((_isWater && (_height_limit < 0 || (_height < _height_limit_half))) || (!_isWater && (_height_limit < 0 || (_height < _height_limit)))) && (_timeout_unsafe < time) && (({ ((_x select 0) distance _ground_pos) <= (_x select 1) } count _unsafe_zones) <= 0);
	if (_hidden) then {
		{
			_unit = _x;
			if !(_unit in _safe) then {
				_safe set [count _safe, _unit];
			};
		} foreach _crew;
		{
			_unit = _x;
			if (!(isNull _unit) && !(captive _unit)) then {
				_unit setCaptive true;
				#ifdef __FORCE_DEBUG__
				_vehicle vehicleChat "Safe";
				#endif
			};
		} foreach _safe;
	} else {
		{
			if (!(isNull _unit)) then {
				_unit setCaptive false;
				#ifdef __FORCE_DEBUG__
				_vehicle vehicleChat "Not safe";
				#endif
			};
		} foreach _safe;
		_safe = [];
	};
	for [{_i = 0},{_i < count _safe},{_i = _i + 1}] do {
		_unit = _safe select _i;
		if (!(isNull _unit) && {!(_unit in _crew)}) then {
			_unit setCaptive false;
			#ifdef __FORCE_DEBUG__
			_unit sideChat "Not safe";
			#endif
			_safe set [_i, "XxDeLxX"];
		};
	};
	_safe = _safe - ["XxDeLxX"]; 

	(isNull _vehicle || {(!alive _vehicle || !canMove _vehicle)})
};
{
	if (!isNull _x) then { _x setCaptive false; };
} foreach _safe;

TRACE("End");